package util

import (
	"crypto/tls"
	"encoding/json"
	"errors"
	"io"
	"net/http"
	"net/url"
	"strconv"
	"strings"
	"time"
)

// 发送post请求,表单，默认5秒,支持https,支持超时时间设置，默认5秒,一般用于表单提交
func Post(_url string, param map[string]interface{}, _times int) (result []byte, err error) {
	__times := time.Second * 1 * time.Duration(_times)
	var client http.Client
	ps := "https://"
	if len(_url) > len(ps) {
		if _url[0:len(ps)] == ps { //是https
			tr := &http.Transport{
				TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
			}
			client = http.Client{Timeout: __times, Transport: tr}
		} else {
			client = http.Client{Timeout: __times}
		}
	} else {
		client = http.Client{Timeout: __times}
	}
	data := url.Values{}
	for k, val := range param {
		data.Set(k, GetAnyString(val))
	}
	resp, err := client.Post(
		_url,
		"application/x-www-form-urlencoded",
		strings.NewReader(data.Encode()))
	if err != nil {
		return result, err
	}
	defer resp.Body.Close()
	if resp.StatusCode != 200 {
		return result, errors.New("状态不对")
	}
	result, err = io.ReadAll(resp.Body)

	return result, err
}

// 发送json
func Json(_url string, param interface{}, _times int) (result []byte, err error) {
	ss, _ := json.Marshal(param)
	__times := time.Second * 1 * time.Duration(_times)
	var client http.Client
	ps := "https://"
	if len(_url) > len(ps) {
		if _url[0:len(ps)] == ps { //是https
			tr := &http.Transport{
				TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
			}
			client = http.Client{Timeout: __times, Transport: tr}
		} else {
			client = http.Client{Timeout: __times}
		}
	} else {
		client = http.Client{Timeout: __times}
	}
	resp, err := client.Post(
		_url,
		"application/json",
		strings.NewReader(string(ss)))
	if err != nil {
		return result, err
	}
	defer resp.Body.Close()
	if resp.StatusCode != 200 {
		return result, errors.New("状态不对")
	}
	body, err := io.ReadAll(resp.Body)
	if err != nil {
		return result, err
	}
	return body, err
}

// 发送get请求
func Get(_url string, _time int) (result []byte, err error) {
	__times := time.Second * 1 * time.Duration(_time)
	var client http.Client
	ps := "https://"
	if len(_url) > len(ps) {
		if _url[0:len(ps)] == ps { //是https
			tr := &http.Transport{
				TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
			}
			client = http.Client{Timeout: __times, Transport: tr}
		} else {
			client = http.Client{Timeout: __times}
		}
	} else {
		client = http.Client{Timeout: __times}
	}

	resp, err := client.Get(_url)
	if err != nil {
		return result, err
	}
	defer resp.Body.Close()
	if resp.StatusCode != 200 {
		return result, errors.New("状态不对")
	}
	body, err := io.ReadAll(resp.Body)
	if err != nil {
		return result, err
	}
	return body, err
}

// 转换任意类型为字符串
func GetAnyString(value interface{}) string {
	var key string
	if value == nil {
		return key
	}

	switch v := value.(type) {
	case float64:
		key = strconv.FormatFloat(v, 'f', -1, 64)
	case float32:
		key = strconv.FormatFloat(float64(v), 'f', -1, 64)
	case int:
		key = strconv.Itoa(v)
	case uint:
		key = strconv.Itoa(int(v))
	case int8:
		it := value.(int8)
		key = strconv.Itoa(int(it))
	case uint8:
		it := value.(uint8)
		key = strconv.Itoa(int(it))
	case int16:
		it := value.(int16)
		key = strconv.Itoa(int(it))
	case uint16:
		it := value.(uint16)
		key = strconv.Itoa(int(it))
	case int32:
		it := value.(int32)
		key = strconv.Itoa(int(it))
	case uint32:
		it := value.(uint32)
		key = strconv.Itoa(int(it))
	case int64:
		it := value.(int64)
		key = strconv.FormatInt(it, 10)
	case uint64:
		it := value.(uint64)
		key = strconv.FormatUint(it, 10)
	case string:
		key = value.(string)
	case []byte:
		key = string(value.([]byte))
	default:
		newValue, _ := json.Marshal(value)
		key = string(newValue)
	}

	return key
}
